package com.sk.gzwater.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.IllegalFormatCodePointException;
import java.util.Locale;

import com.sk.gzwater.service.Settings;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract.Contacts.Data;
import android.util.Log;

public class CommonMethods {	
	
	public static Cursor searchData(Context context,String sql,String[]selectionArgs) {
		DBHelper dbHelper = new DBHelper(context, DBHelper.DATABASE_NAME, null, DBHelper.DATABASE_VERSION);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cr = db.rawQuery(sql, selectionArgs);
		cr.moveToFirst();
		db.close();
		return cr;
	}
	
	public static String[] getDatePeriod(int period) {
		String [] timePeriod  = null ;
		switch (period) {
		case 0:
			timePeriod = getThisWeek();
			break;
		case 1:
			timePeriod = getLastWeek();
			break;
		case 2:
			timePeriod = getThisMonth();
			break;
		case 3:
			timePeriod = getLastMonth();
			break;
		case 4:
			timePeriod = getThisSeason();
			break;
		case 5:
			timePeriod = getLastSeason();
			break;
		case 6:
			timePeriod = getThisYear();
			break;
		case 7:
			timePeriod = getLastYear();
			break;

		default:
			break;
		}
		return timePeriod;
	}

	public static String [] getThisWeek(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int todayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		if(todayInWeek == 0){
			calendar.add(Calendar.DAY_OF_YEAR, -6);
		}else {
			calendar.add(Calendar.DAY_OF_YEAR, -(todayInWeek-2));
		}
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getLastWeek(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int todayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.add(Calendar.DAY_OF_YEAR, -(todayInWeek-1));
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
			calendar.add(Calendar.DAY_OF_YEAR, -6);
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getThisMonth(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int todayInMonth = calendar.get(Calendar.DAY_OF_MONTH);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_YEAR, -(todayInMonth-1));
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getLastMonth(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int todayInMonth = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DAY_OF_YEAR, -todayInMonth);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		calendar.set(Calendar.DAY_OF_MONTH,1);
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getThisSeason(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int month = calendar.get(Calendar.MONTH);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		if(month<3){
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}else if( month>=3 &&month<6){
			calendar.set(Calendar.MONTH, 3);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}else if(month>=6 &&month<9){
			calendar.set(Calendar.MONTH, 6);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}else if(month>=9 &&month<12){
			calendar.set(Calendar.MONTH, 9);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getLastSeason(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int month = calendar.get(Calendar.MONTH);
		if(month<3){
			calendar.add(Calendar.YEAR, -1);
			calendar.set(Calendar.MONTH, 9);
		}else if( month>=3 &&month<6){
			calendar.set(Calendar.MONTH, 0);

		}else if(month>=6 &&month<9){
			calendar.set(Calendar.MONTH, 3);

		}else if(month>=9 &&month<12){
			calendar.set(Calendar.MONTH, 6);			
		}
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		calendar.add(Calendar.MONTH, 3);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		timePeriod[0]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getThisYear(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	public static String [] getLastYear(){
		String [] timePeriod = new String[2];
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int dayInYear = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.add(Calendar.DAY_OF_YEAR, -dayInYear);
		timePeriod[0] = Settings.DATE_FORMAT.format(calendar.getTime());
		calendar.set(Calendar.DAY_OF_MONTH,0);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		timePeriod[1]=Settings.DATE_FORMAT.format(calendar.getTime());
		return timePeriod;
	}
	
	public static void changeCalendar(Calendar clST,Calendar clEND, Date stDate,Date endDate,int timePeriod){		
		try {
			stDate = Settings.DATE_FORMAT.parse(CommonMethods.getDatePeriod(timePeriod)[1]);
			clST = Calendar.getInstance(Locale.CHINA);
			clST.setTime(stDate);
			endDate = Settings.DATE_FORMAT.parse(CommonMethods.getDatePeriod(timePeriod)[0]);
			clEND = Calendar.getInstance(Locale.CHINA);
			clEND.setTime(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	} 
	
}
